package com.zero.xiaodai.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.zero.xiaodai.service.EmailService;
import com.zero.xiaodai.vo.EmailMessage;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;


@Service
@Transactional(propagation = Propagation.REQUIRED)
public class EmailServiceImpl implements EmailService {

	@Autowired
	private JavaMailSenderImpl mailSender;

	@Autowired
	private Configuration freeMarker;

	@Autowired
	private TaskExecutor taskExecutor;


	@Override
	public void sendEmail(String userEmail,int type) throws MessagingException, TemplateNotFoundException,
			MalformedTemplateNameException, ParseException, IOException, TemplateException {


		EmailMessage message=new EmailMessage();
		message.setToEmail(userEmail);
		message.setFromEmail("soultour@vip.163.com");
		StringBuffer str=new StringBuffer();
		for(int i=0;i<4;i++) {
			Random rnd = new Random();
	        int code = rnd.nextInt(10);
	        str.append(String.valueOf(code));
		}
		message.setContent("验证码："+str.toString());
		if(type==0) {
			message.setSubject("忘记密码邮件验证码");
		}else if(type==1) {
			message.setSubject("注册用户邮件验证码");
		}
		
		
		// 获得一个要设置的消息对象
		final MimeMessage mimeMessage = mailSender.createMimeMessage();

		// 获得一个帮助 设置消息对象的对象
		MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");

		// 添加信息
		mimeMessageHelper.setFrom(message.getFromEmail());
		mimeMessageHelper.setTo(message.getToEmail());
		mimeMessageHelper.setSubject(message.getSubject());

		// 获取模板实例
		Template template = freeMarker.getTemplate("mail.ftl");
		// 通过Map传递动态数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("content", message.getContent());
		map.put("name", message.getToEmail());
		map.put("type", type==0?"忘记密码":"注册用户");

		// 解析模板文件
		String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

		mimeMessageHelper.setText(htmlText, true);

		/*Zero smokerUser=smokerUserDao.queryObject(new Zero(userEmail,null));
		smokerUser.setValidateCode(str.toString());
		smokerUser.setValidateDeadline(System.currentTimeMillis()+10*60*1000);
		smokerUserDao.update(smokerUser);*/
		mailSender.send(mimeMessage);
		

	}

}
